<template>
  <div>
    <a-card :bordered="false"
            :bodyStyle="tstyle">
      <div class="table-page-search-wrapper"
           v-if="hasPerm('tableField:page')">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="8"
                   :sm="24">
              <a-form-item label="数据库内表id" v-if="1">
                <a-input v-model="queryParam.tableId"
                         allow-clear
                         placeholder="请输入数据库内表id" />
              </a-form-item>
            </a-col>
            <a-col :md="8"
                   :sm="24">
              <a-form-item label="指标名称id">
                <a-input v-model="queryParam.indicatorsNameId"
                         allow-clear
                         placeholder="请输入指标名称id" />
              </a-form-item>
            </a-col>
            <template v-if="advanced">
              <a-col :md="8"
                     :sm="24">
                <a-form-item label="主数据指标id">
                  <a-input v-model="queryParam.masterDataId"
                           allow-clear
                           placeholder="请输入主数据指标id" />
                </a-form-item>
              </a-col>
              <a-col :md="8"
                     :sm="24">
                <a-form-item label="字段权限分级">
                  <a-input v-model="queryParam.columnPermissions"
                           allow-clear
                           placeholder="请输入字段权限分级" />
                </a-form-item>
              </a-col>
              <a-col :md="8"
                     :sm="24">
                <a-form-item label="字段名">
                  <a-input v-model="queryParam.columnName"
                           allow-clear
                           placeholder="请输入字段名" />
                </a-form-item>
              </a-col>
              <a-col :md="8"
                     :sm="24">
                <a-form-item label="字段备注">
                  <a-input v-model="queryParam.cloumnRemarks"
                           allow-clear
                           placeholder="请输入字段备注" />
                </a-form-item>
              </a-col>
              <a-col :md="8"
                     :sm="24">
                <a-form-item label="字段类型">
                  <a-input v-model="queryParam.cloumnType"
                           allow-clear
                           placeholder="请输入字段类型" />
                </a-form-item>
              </a-col>
              <a-col :md="8"
                     :sm="24">
                <a-form-item label="字段长度">
                  <a-input v-model="queryParam.cloumnLength"
                           allow-clear
                           placeholder="请输入字段长度" />
                </a-form-item>
              </a-col>
              <a-col :md="8"
                     :sm="24">
                <a-form-item label="索引">
                  <a-input v-model="queryParam.cloumnIndex"
                           allow-clear
                           placeholder="请输入索引" />
                </a-form-item>
              </a-col>
              <a-col :md="8"
                     :sm="24">
                <a-form-item label="字段所在的Catalog名字">
                  <a-input v-model="queryParam.cloumnCatalogName"
                           allow-clear
                           placeholder="请输入字段所在的Catalog名字" />
                </a-form-item>
              </a-col>
              <a-col :md="8"
                     :sm="24">
                <a-form-item label="字段小数点后的位数">
                  <a-input v-model="queryParam.cloumnScale"
                           allow-clear
                           placeholder="请输入字段小数点后的位数" />
                </a-form-item>
              </a-col>
              <a-col :md="8"
                     :sm="24">
                <a-form-item label="字段是否自动递增">
                  <a-input v-model="queryParam.cloumnIsAuto"
                           allow-clear
                           placeholder="请输入字段是否自动递增" />
                </a-form-item>
              </a-col>
              <a-col :md="8"
                     :sm="24">
                <a-form-item label="字段是否是主键">
                  <a-input v-model="queryParam.cloumnIsPrimary"
                           allow-clear
                           placeholder="请输入字段是否是主键" />
                </a-form-item>
              </a-col>
              <a-col :md="8"
                     :sm="24">
                <a-form-item label="字段在数据库中是否为货币型">
                  <a-input v-model="queryParam.cloumnIsCurrency"
                           allow-clear
                           placeholder="请输入字段在数据库中是否为货币型" />
                </a-form-item>
              </a-col>
              <a-col :md="8"
                     :sm="24">
                <a-form-item label="字段是否为空">
                  <a-input v-model="queryParam.cloumnIsNullable"
                           allow-clear
                           placeholder="请输入字段是否为空" />
                </a-form-item>
              </a-col>
              <a-col :md="8"
                     :sm="24">
                <a-form-item label="字段是否为只读">
                  <a-input v-model="queryParam.cloumnIsReadonly"
                           allow-clear
                           placeholder="请输入字段是否为只读" />
                </a-form-item>
              </a-col>
              <a-col :md="8"
                     :sm="24">
                <a-form-item label="字段是否出现在where中">
                  <a-input v-model="queryParam.cloumnIsSearchable"
                           allow-clear
                           placeholder="请输入字段是否出现在where中" />
                </a-form-item>
              </a-col>
              <a-col :md="8"
                     :sm="24">
                <a-form-item label="访问权限">
                  <a-input v-model="queryParam.permissions"
                           allow-clear
                           placeholder="请输入访问权限" />
                </a-form-item>
              </a-col>
            </template>
            <a-col :md="8"
                   :sm="24">
              <span class="table-page-search-submitButtons">
                <a-button type="primary"
                          @click="$refs.table.refresh(true)">查询</a-button>
                <a-button style="margin-left: 8px"
                          @click="() => queryParam = {}">重置</a-button>
                <a @click="toggleAdvanced"
                   style="margin-left: 8px">
                  {{ advanced ? '收起' : '展开' }}
                  <a-icon :type="advanced ? 'up' : 'down'" />
                </a>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </a-card>
    <a-card :bordered="false">
      <s-table ref="table"
               :columns="columns"
               :data="loadData"
               :alert="options.alert"
               :rowKey="(record) => record.id"
               :rowSelection="options.rowSelection">
        <template class="table-operator"
                  slot="operator"
                  v-if="hasPerm('tableField:add')">
          <!-- <a-button type="primary" v-if="hasPerm('tableField:add')" icon="plus" @click="$refs.addForm.add()">新增数据库信息表</a-button> -->
          <a-button type="danger"
                    :disabled="selectedRowKeys.length < 1"
                    v-if="hasPerm('tableField:delete')"
                    @click="batchDelete">
            <a-icon type="delete" />批量删除
          </a-button>
          <x-down v-if="hasPerm('tableField:export')"
                  ref="batchExport"
                  @batchExport="batchExport" />
        </template>
        <span slot="action"
              slot-scope="text, record">
          <a v-if="hasPerm('tableField:edit')"
             @click="$refs.editForm.edit(record)">编辑</a>
          <a-divider type="vertical"
                     v-if="hasPerm('tableField:edit') & hasPerm('tableField:delete')" />
          <a-popconfirm v-if="hasPerm('tableField:delete')"
                        placement="topRight"
                        title="确认删除？"
                        @confirm="() => singleDelete(record)">
            <a>删除</a>
          </a-popconfirm>
        </span>
      </s-table>
      <add-form ref="addForm"
                @ok="handleOk" />
      <edit-form ref="editForm"
                 @ok="handleOk" />
    </a-card>
  </div>
</template>
<script>
import { STable, XDown } from '@/components'
let eventBus = () => import('../../../utils/eventBus/eventBus.js')

import { tableFieldPage, tableFieldDelete, tableFieldExport } from '@/api/modular/main/tablefield/tableFieldManage'
import addForm from './addForm.vue'
import editForm from './editForm.vue'
export default {
  components: {
    STable,
    addForm,
    editForm,
    XDown
  },
  data() {
    return {
      loadDatalist:[],
      // 高级搜索 展开/关闭
      advanced: false,
      // 查询参数
      queryParam: {},
      // 表头
      columns: [
        {
          //todo  
          title: '表名',
          align: 'center',
          dataIndex: 'tableId'
        },
        // {
        //   title: '指标名称id',
        //   align: 'center',
        //   dataIndex: 'indicatorsNameId'
        // },
        // {
        //   title: '主数据指标id',
        //   align: 'center',
        //   dataIndex: 'masterDataId'
        // },
        // {
        //   title: '字段权限分级',
        //   align: 'center',
        //   dataIndex: 'columnPermissions'
        // },
        {
          title: '字段名',
          align: 'center',
          dataIndex: 'columnName'
        },
        {
          title: '字段备注',
          align: 'center',
          dataIndex: 'cloumnRemarks'
        },
        {
          title: '字段类型',
          align: 'center',
          dataIndex: 'cloumnType'
        },
        {
          title: '字段长度',
          align: 'center',
          dataIndex: 'cloumnLength'
        },
        {
          title: '索引',
          align: 'center',
          dataIndex: 'cloumnIndex'
        },
        {
          title: '字段所在的Catalog名字',
          align: 'center',
          dataIndex: 'cloumnCatalogName'
        },
        {
          title: '字段小数点后的位数',
          align: 'center',
          dataIndex: 'cloumnScale'
        },
        {
          title: '字段是否自动递增',
          align: 'center',
          dataIndex: 'cloumnIsAuto'
        },
        {
          title: '字段是否是主键',
          align: 'center',
          dataIndex: 'cloumnIsPrimary'
        },
        {
          title: '字段在数据库中是否为货币型',
          align: 'center',
          dataIndex: 'cloumnIsCurrency'
        },
        {
          title: '字段是否为空',
          align: 'center',
          dataIndex: 'cloumnIsNullable'
        },
        {
          title: '字段是否为只读',
          align: 'center',
          dataIndex: 'cloumnIsReadonly'
        },
        {
          title: '字段是否出现在where中',
          align: 'center',
          dataIndex: 'cloumnIsSearchable'
        },
        {
          title: '访问权限',
          align: 'center',
          dataIndex: 'permissions'
        }
      ],
      tstyle: { 'padding-bottom': '0px', 'margin-bottom': '10px' },
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        return tableFieldPage(Object.assign(parameter, this.queryParam)).then((res) => {
          console.log('11a', res.data)
          return res.data
        })
      },
      selectedRowKeys: [],
      selectedRows: [],
      options: {
        alert: { show: true, clear: () => { this.selectedRowKeys = [] } },
        rowSelection: {
          selectedRowKeys: this.selectedRowKeys,
          onChange: this.onSelectChange
        }
      }
    }
  },
  created() {
    if (this.hasPerm('tableField:edit') || this.hasPerm('tableField:delete')) {
      this.columns.push({
        title: '操作',
        width: '150px',
        dataIndex: 'action',
        scopedSlots: { customRender: 'action' }
      })
    }


  },
  mounted() {
    eventBus().then(res => {
      console.log(res.eventBus)
      // this.loadData= ()=>{
      //   res.eventBus.$on('change', e => {
      //   console.log('heard', e)
      //   return e
      // })
      // }
     
    })
    console.log('12300')
    // this.data.loadDatalist=this.loadData('')

  },

  methods: {
    /**
     * 单个删除
     */
    singleDelete(record) {
      const param = [{ 'id': record.id }]
      this.tableFieldDelete(param)
    },
    /**
     * 批量删除
     */
    batchDelete() {
      const paramIds = this.selectedRowKeys.map((d) => {
        return { 'id': d }
      })
      this.tableFieldDelete(paramIds)
    },
    tableFieldDelete(record) {
      tableFieldDelete(record).then((res) => {
        if (res.success) {
          this.$message.success('删除成功')
          this.$refs.table.clearRefreshSelected()
        } else {
          this.$message.error('删除失败') // + res.message
        }
      })
    },
    toggleAdvanced() {
      this.advanced = !this.advanced
    },
    /**
     * 批量导出
     */
    batchExport() {
      const paramIds = this.selectedRowKeys.map((d) => {
        return { 'id': d }
      })
      tableFieldExport(paramIds).then((res) => {
        this.$refs.batchExport.downloadfile(res)
      })
    },
    handleOk() {
      this.$refs.table.refresh()
    },
    onSelectChange(selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    }
  }
}
</script>
<style lang="less">
.table-operator {
  margin-bottom: 18px;
}

button {
  margin-right: 8px;
}
</style>
